package com.quan.hr.web.controller;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
import cn.afterturn.easypoi.exception.excel.ExcelImportException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.commons.exception.BizException;
import com.quan.commons.interceptor.LoginUserInterceptor;
import com.quan.commons.vo.R;
import com.quan.commons.core.MyBaseController;
import com.quan.commons.utils.PageUtils;
import com.quan.commons.annotation.OperationLog;

import com.quan.hr.commons.export.HrPostImportVo;
import com.quan.system.commons.vo.SysUserVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.quan.hr.commons.vo.HrPostVo;
import com.quan.hr.entity.HrPost;
import com.quan.hr.service.HrPostService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;


/**
 * 人事管理-岗位信息
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2023-02-21 16:41:34
 */
@Slf4j
@Api(tags = "人事管理-岗位信息")
@RestController
@RequestMapping("/hr/hrpost")
public class HrPostController extends MyBaseController {

    @Autowired
    private HrPostService service;

    /**
     * 保存
     * @param vo
     * @return
     */
    @OperationLog(module = "人事管理-岗位信息", title = "新增")
    @PostMapping("save")
    public R save(@Valid @RequestBody HrPostVo vo) {
        this.service.save(vo);
        return R.ok();
    }

    /**
     * 修改
     * @param vo
     * @return
     */
    @OperationLog(module = "人事管理-岗位信息", title = "修改")
    @PostMapping("update")
    public R update(@Valid @RequestBody HrPostVo vo) {
        final SysUserVo user = LoginUserInterceptor.loginUser.get();
        vo.setUname(null != user.getEmpName() ? user.getEmpName() : user.getUsername());
        this.service.updateById(vo);
        return R.ok();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @OperationLog(module = "人事管理-岗位信息", title = "删除")
    @PostMapping("delete")
    public R delete(@RequestBody Long[] ids) {
        if (null != ids && ids.length > 0) {
            this.service.removeByIds(Arrays.asList(ids));
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 多条件查询信息详情
     * @param vo
     * @return
     */
    @OperationLog(module = "人事管理-岗位信息", title = "查询详情")
    @GetMapping("/info")
    public R info(HrPostVo vo) {
        QueryWrapper<HrPost> queryWrapper = new QueryWrapper<HrPost>(vo);
        HrPost hrPost =this.service.getOne(queryWrapper);
        return R.ok().data(hrPost);
    }

    /**
     * 根据ID查询信息详情
     * @param {id}
     * @return
     */
    @OperationLog(module = "人事管理-岗位信息", title = "查询详情")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        HrPost hrPost =this.service.getInfo(id);
        return R.ok().data(hrPost);
    }

    /**
     * 列表查询
     * @param vo
     * @return
     */
    @OperationLog(module = "人事管理-岗位信息", title = "查询列表")
    @GetMapping(value = "list")
    public Object list(HrPostVo vo) {
        List<HrPostVo> list = this.service.queryList(new PageUtils(request), vo);
        return R.ok().data(list);
    }

    /**
     * 分页查询
     * @param vo
     * @return
     */
    @OperationLog(module = "人事管理-岗位信息", title = "查询列表")
    @GetMapping(value = "page")
    public R queryPage(HrPostVo vo) {
        PageUtils page = this.service.queryPage(new PageUtils(request), vo);
        return R.ok().data(page);
    }

    /**
     * 树形结构查询
     * @return
     */
    @OperationLog(module = "岗位信息", title = "树形结构查询")
    @GetMapping(value = "/listWithTree")
    public R listWithTree() {
        return R.ok().put("data", this.service.listWithTree());
    }

    /**
     * 单文件上传
     * 导入岗位Excel数据
     * @param request
     * @return
     */
    @OperationLog(module = "岗位信息", title = "导入岗位Excel数据")
    @PostMapping(value = "/importExcelPost")
    public R importExcelPost(@RequestParam(value = "file")MultipartFile multipartFile, HttpServletRequest request) throws Exception {
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();

            ImportParams params = new ImportParams();

            final List<HrPostImportVo> list = new ExcelImportService().importExcelByIs(inputStream, HrPostImportVo.class, params, false).getList();

            // 保存数据
            this.service.saveImportData(list);

        } finally {
            IOUtils.closeQuietly(inputStream);
        }

        return R.ok();
    }

}
